package skiree.host.abetsy.api.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Component;
import skiree.host.abetsy.api.dao.AnimeDao;
import skiree.host.abetsy.api.dao.CommentDao;
import skiree.host.abetsy.api.dao.ConfigDao;
import skiree.host.abetsy.api.dao.EpisodeDao;
import skiree.host.abetsy.api.service.EpisodeService;
import skiree.host.abetsy.api.service.global.GlobalDIYService;
import skiree.host.abetsy.api.vessel.Result;
import skiree.host.abetsy.model.LocalAnime;
import skiree.host.abetsy.model.LocalComment;
import skiree.host.abetsy.model.LocalConfig;
import skiree.host.abetsy.model.LocalEpisode;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EpisodeServiceImpl implements EpisodeService {

    @Resource
    private GlobalDIYService globalDIYService;

    @Resource
    private EpisodeDao episodeDao;

    @Resource
    private AnimeDao animeDao;

    @Resource
    private CommentDao commentDao;

    @Resource
    private ConfigDao configDao;

    private final Object lockRefresh = new Object();

    @Override
    public Result list(int animeId, String query, int pageNum, int pageSize) {
        // 查询排序设置
        QueryWrapper<LocalConfig> configQuery = new QueryWrapper<>();
        configQuery.eq("name", "episodeSort");
        LocalConfig localConfig = configDao.selectOne(configQuery);
        boolean sortType = localConfig == null || Boolean.parseBoolean(localConfig.getValue());
        // 分页信息
        Page<LocalEpisode> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LocalEpisode> episodeQuery = new QueryWrapper<>();
        episodeQuery.eq("anime_id", animeId);
        episodeQuery.like("title", query);
        if (sortType) {
            episodeQuery.orderByDesc("episode_index");
        } else {
            episodeQuery.orderByAsc("episode_index");
        }
        IPage<LocalEpisode> episodePage = episodeDao.selectPage(page, episodeQuery);
        Map<String, Object> data = new HashMap<>();
        data.put("total", episodePage.getTotal());
        data.put("pageNum", pageNum);
        data.put("pageSize", pageSize);
        data.put("episode", episodePage.getRecords());
        return Result.success(data);
    }

    @Override
    public Result shows(int animeId) {
        //查询显示的标题
        QueryWrapper<LocalAnime> animeQuery = new QueryWrapper<>();
        animeQuery.eq("id", animeId);
        LocalAnime localAnime = animeDao.selectOne(animeQuery);
        String shows = "";
        if (localAnime != null) {
            shows = localAnime.getTitle() + " 第" + Convert.numberToChinese(localAnime.getSeason(), false) + "季";
        }
        Map<String, Object> data = new HashMap<>();
        data.put("shows", shows);
        return Result.success(data);
    }

    @Override
    public Result add(LocalEpisode localEpisode) {
        QueryWrapper<LocalEpisode> episodeQuery = new QueryWrapper<>();
        episodeQuery.eq("episode_index", localEpisode.getEpisodeIndex());
        episodeQuery.eq("anime_id", localEpisode.getAnimeId());
        LocalEpisode episodeDb = episodeDao.selectOne(episodeQuery);
        if (episodeDb != null) {
            return Result.error(501, "此剧集数已存在!");
        }
        if (localEpisode.getUrl().isEmpty()) {
            return Result.error(501, "必须设置剧集播放源!");
        }
        localEpisode.setTime(new Date());
        localEpisode.setStatus("init");
        episodeDao.insert(localEpisode);
        return Result.success(localEpisode);
    }

    @Override
    public Result edit(LocalEpisode localEpisode) {
        QueryWrapper<LocalEpisode> episodeQuery = new QueryWrapper<>();
        episodeQuery.eq("episode_index", localEpisode.getEpisodeIndex());
        episodeQuery.eq("anime_id", localEpisode.getAnimeId());
        episodeQuery.ne("id", localEpisode.getId());
        LocalEpisode episodeCk = episodeDao.selectOne(episodeQuery);
        if (episodeCk != null) {
            return Result.error(501, "此剧集数已存在!");
        }
        if (localEpisode.getUrl().isEmpty()) {
            return Result.error(501, "必须设置剧集播放源!");
        }
        LocalEpisode episodeDb = episodeDao.selectById(localEpisode.getId());
        episodeDb.setTitle(localEpisode.getTitle());
        episodeDb.setEpisodeIndex(localEpisode.getEpisodeIndex());
        episodeDb.setUrl(localEpisode.getUrl());
        episodeDao.updateById(episodeDb);
        return Result.success(episodeDb);
    }

    @Override
    public void delete(int id) {
        episodeDao.deleteById(id);
        QueryWrapper<LocalComment> commentQuery = new QueryWrapper<>();
        commentQuery.eq("episode_id", id);
        commentDao.delete(commentQuery);
    }

    @Override
    public void refresh(int id) {
        synchronized (lockRefresh) {
            LocalEpisode episode = episodeDao.selectById(id);
            globalDIYService.loadDanMu(episode);
        }
    }
}
